﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using UnityEngine;

namespace SimpleTools.Json
{
    /// <summary>
    /// 获取数字类型时，应使用快速获取方法
    /// </summary>
    public class JsonData
    {
        /// <summary>
        /// 字典，可以根据字符串取值
        /// </summary>
        [JsonProperty]
        private Dictionary<string, object> Dic = new Dictionary<string, object>();

        /// <summary>
        /// 获取该与该对象对应的JSON字符串
        /// </summary>
        [JsonIgnore]
        public string Json { get => JsonConvert.SerializeObject(this); }


        /// <summary>
        /// 初始化对象
        /// </summary>
        public JsonData()
        {

        }

        /// <summary>
        /// 初始化对象并添加action键值
        /// </summary>
        /// <param name="action">键action的值</param>
        public JsonData(string action)
        {
            Dic.Add("action", action);
        }

        /// <summary>
        /// 判断json中是否含有键
        /// </summary>
        /// <param name="keys">键，不定个数</param>
        /// <returns>json中是否含有指定的键</returns>
        public bool IsContainKeys(params string[] keys)
        {
            foreach (var item in keys)
            {
                if (!Dic.ContainsKey(item))
                {
                    return false;
                }
            }
            return true;
        }

        public bool IsContainKey(string key)
        {
            return Dic.ContainsKey(key);
        }

        #region 快速获取方法
        public string GetStr(string key) { return Dic[key].ToString(); }
        public uint GetUint(string key) { return Convert.ToUInt32(Dic[key]); }
        public ulong GetULong(string key) { return Convert.ToUInt64(Dic[key]); }
        public int GetInt(string key) { return Decimal.ToInt(Convert.ToInt64(Dic[key])); }
        public long GetLong(string key) { return Convert.ToInt64(Dic[key]); }
        public float GetFloat(string key) { return Decimal.ToFloat(Convert.ToInt64(Dic[key])); }
        public double GetDouble(string key) { return Decimal.ToDouble(Convert.ToInt64(Dic[key])); }
        public bool GetBool(string key) { return Convert.ToBoolean(Dic[key]); }
        public object GetObject(string key) { return Dic[key]; }
        public bool IsSucc() { return Convert.ToBoolean(Dic["IsSucc"]); }
        public override string ToString()
        {
            string s = "";
            foreach (var item in Dic)
            {
                s += item.Key + ":" + item.Value + "%%";
            }
            return s;
        }
        #endregion

        #region 使用方法
        public void Add(string key, string value)
        {
            Dic.Add(key, value);
        }
        public void Add(string key, ulong value)
        {
            Dic.Add(key, value);
        }
        public void Add(string key, int value)
        {
            Dic.Add(key, new Decimal(value).value);
        }
        public void Add(string key, float value)
        {
            Dic.Add(key, new Decimal(value).value);
        }
        public void Add(string key, double value)
        {
            Dic.Add(key, new Decimal(value).value);
        }
        public void Add(string key, long value)
        {
            Dic.Add(key, value);
        }
        public void Add(string key, uint value)
        {
            Dic.Add(key, value);
        }
        public void Add(string key, bool value)
        {
            Dic.Add(key, value);
        }
        public void Add(string key, object value)
        {
            Dic.Add(key, value);
        }

        public void remove(string key)
        {
            Dic.Remove(key);
        }
        #endregion



    }
}
